﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CharmL {
    public class Eqclass {
        public List<int> Prefix { get; set; }
        public List<Eqnode> nList { get; set; }
        public List<int> OtherFreqNodes { get; set; }

        public Eqclass() {
            Prefix = new List<int>();
            nList = new List<Eqnode>();
            OtherFreqNodes = new List<int>();
        }
        public void SortNodes() {
            if (Paramenters.sort_type == sort_vals.incr) {
                nList.Sort(new IncrCmpComparer());
            } else if (Paramenters.sort_type == sort_vals.decr) {
                nList.Sort(new DecrCmpComparer());
            }
        }

        public void AddNode(int val) {
            //        void Eqclass::add_node(int val)
            //{
            //   Eqnode *eqn = new Eqnode(val);
            //   _nodelist.push_back(eqn);
            //}
            Eqnode eqn = new Eqnode(val);
            nList.Add(eqn);
        }

        public void AddNode(Eqnode eqn) {
            //void Eqclass::add_node(Eqnode *eqn)
            //{
            //   _nodelist.push_back(eqn);
            //}
            nList.Add(eqn);
        }

        public void SetPrefix(List<int> pref, Eqnode node) {
            //        void Eqclass::set_prefix(vector<int> &pref, Eqnode &node)
            //{
            //   _prefix = pref;
            //   _prefix.push_back(node.val);
            //}
            foreach (int item in pref) {
                Prefix.Add(item);
            }
            //Prefix = pref;
            Prefix.Add(node.Val);
        }

        public void PrintPrefix(bool supFlg = false) {
            //TODO
            for (int i = 0; i < Prefix.Count; i++) {
                //Console.Write(Dbase_Ctrl_Blk.FreqIdx[Prefix[i]] + " ");
            }
            //ostream & Eqclass::print_prefix(bool supflg)
            //{
            //   for (unsigned int i=0; i < _prefix.size(); ++i){
            //      cout << Dbase_Ctrl_Blk::FreqIdx[_prefix[i]] << " ";
            //   }
            //   //if (supflg) cout << " - " << _closedsup << " ";
            //   return cout;
            //}
        }

        public void PrintPrefix(StreamWriter writer, bool printLast = false) {
            //TODO
            for (int i = 0; i < Prefix.Count - 1; i++) {
                //Console.Write(Dbase_Ctrl_Blk.FreqIdx[Prefix[i]] + " ");
            }

            //        ostream & Eqclass::print_prefix(ostream& fout, bool printlast)
            //{
            //   unsigned int i=0;
            //   for (i=0; i < _prefix.size()-1; ++i){
            //      fout << Dbase_Ctrl_Blk::FreqIdx[_prefix[i]] << " ";
            //   }
            //   if (printlast)
            //      fout << Dbase_Ctrl_Blk::FreqIdx[_prefix[i]] << " ";

            //   return fout;
            //}
        }


        public void PrintNode(StreamWriter writer, Eqnode node) {
            //        ostream & Eqclass::print_node(ostream &fout, Eqnode &node)
            //{
            //   print_prefix(fout);
            //   fout << node;
            //   return fout;
            //}
            PrintPrefix(writer);
            writer.Write(node.ToString());
        }

        public override string ToString() {
            StringBuilder builder = new StringBuilder(string.Empty);
            for (int i = 0; i < nList.Count; i++) {
                builder.AppendLine(nList[i].ToString());
            }
            return builder.ToString();

            //        //print with items remapped to their original value
            //ostream& operator << (ostream& fout, Eqclass& eq)
            //{
            //  list<Eqnode *>::iterator ni = eq._nodelist.begin();
            //  for (; ni != eq._nodelist.end(); ++ni){
            //     eq.print_node(fout, *(*ni)) << endl;
            //  }
            //  return fout;
            //}
        }



        //        class Eqclass{
        //private:
        //   vector<int> _prefix;
        //   //int _closedsup; //used when mining closed sets
        //   list<Eqnode *> _nodelist;
        //   vector<int> _otherFreqNodes; //nodelist contains the freq elements
        //                                //that are lex greater than 

        //public:

        //   ~Eqclass();
        //   vector<int> &prefix(){ return _prefix; }
        //   list<Eqnode *> &nlist(){ return _nodelist; }
        //   //int &closedsupport(){ return _closedsup; }


        //   void sort_nodes(){
        //      if (sort_type == incr) _nodelist.sort(Eqnode::incr_cmp);
        //      else if (sort_type == decr) _nodelist.sort(Eqnode::decr_cmp);
        //   }

        //   void add_node(int val);
        //   void add_node(Eqnode *eqn);
        //   int item(int n);
        //   void set_prefix(vector<int> &pref, Eqnode &node);
        //   ostream & print_prefix(bool supflg=false);
        //   ostream & print_prefix(ostream& fout, bool printlast=true);
        //   ostream & print_node(ostream& fout, Eqnode &node);
        //   friend ostream & operator<<(ostream& ostr, Eqclass& eq);
        //};
    }
}
